package cn.it.shop.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.it.shop.pojo.Goods;

@Controller(value = "goodsAction")
@Scope("prototype")
public class GoodsAction extends BaseAction<Goods> {

	private File file;
	private String contentType;
	private String filename;

	public void setUpload(File file) {
		this.file = file;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String filename) {
		this.filename = filename;
	}

	/*
	 * 1: 表单必须post提交,而且是多媒体表单 2：在Action中配置接收文件信息的参数
	 */

	public String save() {
		System.out.println(model.getCategory().getType());
		// 调用文件上传的功能,返回的是新的文件名
		String pic = fileUploadUtil.uploadFile(file, filename);
		// 把新文件名存储到数据中
		model.setPic(pic);
		// 必须要先添加数据库在添加索引库,而且两个应该在同一个事物中
		goodsService.save(model);
		// luceneService.addGoods(model);
		return "query";
	}

	public String query$() {
		List<Goods> goodsList = goodsService.queryByName(model.getName());
		System.out.println(goodsList);
		request.put("goodsList", goodsList);
		return "search";
	}

	public String get$() {
		// 根据商品ID获取商品的详细信息,可以把goods配置到二级缓存中
		request.put("goods", goodsService.get(model.getId()));
		return "detail";
	}
}
